package com.beeasy.hzbpm.ctrl;

import cn.hutool.core.util.StrUtil;
import com.beeasy.hzbpm.filter.Auth;
import com.beeasy.hzbpm.util.Result;
import com.github.llyb120.json.Obj;
import com.github.llyb120.nami.server.Cookie;

import java.util.Date;

import static com.beeasy.hzbpm.bean.MongoService.db;
import static com.github.llyb120.json.Json.o;
import static com.github.llyb120.json.Json.ooo;

public class login {

    public Object login(String username, String password, Cookie cookie){
        Obj user = ooo(db.getCollection("user")
            .find(o(
                "username", username,
                "password", password
            ).toBson())
            .projection(o(
                "_id",0,
                "id", 1,
                "username",1,
                "true_name",1
            ).toBson())
            .first());
        if(user.isEmpty()){
            return Result.error("用户名或密码错误");
        }
        String token = StrUtil.uuid();
//        cookie.set("bpm-token", token);
        db.getCollection("userToken").insertOne(o("uid", user.s("id"), "token", token, "createDate", new Date()).toBson());
        return Result.ok(o(
            "user", user,
            "token", token
        ));
    }

    public Object checkLogin(){
        String uid = Auth.getUid();
//        System.err.println(uid);
        return Result.ok(!uid.equals("-1"));
    }


}
